<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>用户登录</title>
<link rel="stylesheet" type="text/css" href="css/reset.css" />
<link rel="stylesheet" type="text/css" href="css/css.css" />
<link rel="stylesheet" type="text/css"
	href="js/jquery-easyui-1.4.3/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css"
	href="js/jquery-easyui-1.4.3/themes/icon.css" />
<link rel="stylesheet" type="text/css"
	href="js/SimpleTree1.3/js/tree_themes/SimpleTree.css" />
<link rel="stylesheet" type="text/css" href="../../css/colorReset.css" />
<style>
html {
	background: url(images/body_bg.jpg) left top repeat-x;
}

body {
	width: 100%;
	height: 100%;
	background: url(images/login_page_bg.jpg) center top no-repeat;
	font-size: 16px;
	color: #fff;
	overflow: auto;
}

a {
	color: #333366;
	font-size: 12px;
}

.title {
	width: 80%;
	margin: 0 auto;
	padding: 30px 0 10px 0;
	font-size: 25px;
	text-align: center;
}

.title h2 img {
	width: 177px;
}

.login_con {
	width: 345px;
	height: 240px;
	padding: 20px;
	margin: 0 auto;
	border: none;
	background: url(images/login_bg.png) center center no-repeat;
}

.login_con form {
	padding-top: 15px;
}

.input_con {
	position: relative;
	vertical-align: middle;
	margin: 15px 32px 0;
}

.input_con>input.text {
	padding-left: 40px;
	padding-right: 5px;
	border-radius: 0;
	border: none;
	background: url(images/login_field_bg.png) center center no-repeat;
	width: 235px;
	height: 40px;
	line-height: 40px;
	vertical-align: middle;
	color: #fff;
	font-size: 14px;
}

.input_con>.placeholder {
	display: block;
	position: absolute;
	left: 43px;
	top: 0;
	color: #cfedff;
	/*color: #fff;
	filter: alpha(opacity=70);
	opacity: .7;*/
	line-height: 42px;
}

.checkbox-label {
	color: #333366;
	margin-left: 5px;
	font-size: 12px;
}

.img-con {
	position: absolute;
	height: 22px;
	width: 22px;
	left: 1px;
	top: 10px;
	padding: 0 6px;
	border: none;
	border-right: 1px solid #fff;
}

.input_con>img {
	
}

.button_con_div a, .button_con_div input[type=submit] {
	height: 40px;
	line-height: 40px;
	vertical-align: middle;
	text-align: center;
	color: #fff;
	width: 130px;
	border: none;
	background: url(images/login_btn_bg.png) center center no-repeat;
	padding: 0;
	font-size: 16px;
	font-family: '微软雅黑', 'Times New Roman';
}

.button_con_div a:hover {
	background: url(images/login_btn_bg_over.png) center center no-repeat;
}

.login_con a:hover {
	color: #fff;
}

.btn-left {
	float: left;
	height: 43px;
	width: 7px;
	background: url(images/login_btn_left.png) center center no-repeat;
}

.btn-center {
	height: 43px;
	margin: 0 7px;
	background: url(images/login_btn_center.png) center center repeat-x;
}

.btn-right {
	float: right;
	height: 43px;
	width: 7px;
	margin-right: -7px;
	background: url(images/login_btn_right.png) center center no-repeat;
}

.clearfix {
	clear: both;
}

.button_con_div {
	margin-top: 10px;
}

#RememberPass {
	display: none;
}

.checkbox-icon {
	height: 15px;
	width: 15px;
	/*background: url(images/uncheck.png) center center no-repeat;*/
}

.checkbox-icon.checked {
	/*background: url(images/check.png) center center no-repeat;*/
	
}
/*.button_con_div a {
	width: 49%;
	text-align:center;
}
.button_con_div a.fl {
	border-right: 1px solid #fff;
}*/
</style>
<script type="text/javascript">
	if(window.parent.location.href != location.href)
	{
		window.parent.location.href = "/UserLogin.html";
	}
		
</script>
</head>
<body>
	<div class="main">
		<div class="title">
			<h2>
				<img src="images/logo_login.png" alt="COOK接口数据管理平台" />
			</h2>
		</div>
		<div class="login_con">
			<form id="loginForm" method="post" action="/api/login">
				<div class="input_con">
					<input id="username" name="username" type="text" class=" text"
						autocomplete="off" missingMessage="账号不能为空！" /> <label
						for="username" class="placeholder">用户名</label>
					<div class="img-con">
						<img src="images/user_bg.png" alt="用户名" />
					</div>
				</div>

				<div class="input_con">
					<input id="password" name="password" type="password" class="text"
						autocomplete="off" missingMessage="密码不能为空！" /> <label
						for="password" class="placeholder">密码</label>
					<div class="img-con">
						<img src="images/password_bg.png" alt="密码" />
					</div>
				</div>

				<div class="input_con">
					<input type="checkbox" id="RememberPass" name="RememberPass" /> <img
						class="checkbox-icon fl" src="images/uncheck.png" alt="记住密码" /> <label
						class="checkbox-label fl">记住密码</label> <a
						href="pages/UserManagement/PassRetrieve.html" target="_self"
						class="fr" id="forget_password">忘记密码</a>
					<div class="clearfix"></div>
				</div>

				<div class="input_con button_con_div">
					<input type="submit" class="login_btn fl" id="login" value="登录" />
					<!-- <a href="javascript:void(0);" class="login_btn fl" id="login">登录</a> -->
					<a href="pages/UserManagement/UserRegistration.html" target="_self"
						class="regist_btn fr" id="register">注册</a>
					<div class="clearfix"></div>
					<!--<button class="login_btn" id="login">
					<div class="btn-left"></div>
					<div class="btn-center">
						<span class="btn-text">登录</span>
						<div class="btn-right"></div>
					</div>
				</button>
			</div>
			 <div class="input_con button_con_div">
			 	<a href="pages/UserManagement/PassRetrieve.html" target="_self" class="fl" id="forget_password">忘记密码</a>
				<a href="pages/UserManagement/UserRegistration.html" target="_self" class="fr" id="register">立即注册</a>
				<div class="clearfix"></div>  -->
				</div>
			</form>
		</div>
	</div>



	<script type="text/javascript" src="/webjars/json3/3.3.2/json3.min.js"></script>
	<script type="text/javascript"
		src="js/jquery-easyui-1.4.3/jquery.min.js"></script>
	<script type="text/javascript"
		src="js/jquery-easyui-1.4.3/jquery.easyui.min.js"></script>
	<script type="text/javascript"
		src="js/jquery-easyui-1.4.3/locale/easyui-lang-zh_CN.js"></script>

	<script type="text/javascript" src="js/cook/constants.js"></script>
	<script type="text/javascript" src="js/cook/FormValidate.js"></script>

	<script type="text/javascript" src="js/rsa/security.js"></script>
	<script type="text/javascript" src="js/rsa/rsaly.js"></script>
	<script>
var g_isNeedEncrypt = true;
$(document).ready(function(){
	$('#username, #password').bind('input propertychange', function() {
	    if($(this).val() != "")
	    {
	    	$(".placeholder", $(this).parent()).hide();
	    }
	    else
	    {
	    	$(".placeholder", $(this).parent()).show();
	    }
	    //clearAutofillStyle();
	});
	/*$(".input_con").delegate('#username, #password','input propertychange', function() {
	    if($(this).val() != "")
	    {
	    	$(".placeholder", $(this).parent()).hide();
	    }
	    else
	    {
	    	$(".placeholder", $(this).parent()).show();
	    }
	    //clearAutofillStyle();
	});*/
	
	/*$("#loginForm").keyup(function(event){
	  if(event.keyCode ==13){
		if(!$("#login").attr("disabled"))
		{
			$("#login").trigger("click");
		}
	    
	  }
	});*/
		
	//$("input").val("");
	
	$("#username").val(getCookie("username"));
	//删除旧版本的cookie
	if(getCookie("password")) {
		$("#password").val(getCookie("password"));
		g_isNeedEncrypt = true;
		delCookie("password");
	}
	if(getCookie("encryptpassword")) {
		//$("#password").val(getCookie("encryptpassword"));
		$("#password").val("");
		g_isNeedEncrypt = true;
		delCookie("encryptpassword");
		delCookie("isRememberPass");
	}
	if(getCookie("encrypt_p")) {
		//$("#password").val(getCookie("encrypt_p"));
		$("#password").val(getRandomFakePsw());
		g_isNeedEncrypt = false;
	}
	
	$(".input_con .text").each(function(){
		if($(this).val() != "")
	    {
	    	$(".placeholder", $(this).parent()).hide();
	    }
	    else
	    {
	    	$(".placeholder", $(this).parent()).show();
	    }
	});
	clearAutofillStyle();
	
	$("#loginForm").form("validate");
		 
	$('#txtCompany').combobox({
		editable: false,
		url: '',
		valueField: 'id',
		textField: 'name',
		method:'get',
		loadFilter:function(data){
			return data.content;
		}
		
	});
	
	$('#txtDepartment').combobox({
		editable: false,
		url: '',
		valueField: 'id',
		textField: 'name',
		method:'get',
		loadFilter:function(data){
			return data.content;
		}
		
	});
	
	$(".checkbox-icon,.checkbox-label").click(function(){
		if($("#RememberPass").is(":checked"))
		{
			$("#RememberPass").removeAttr("checked");
			//$(".checkbox-icon").removeClass("checked");
			$(".checkbox-icon").attr("src","images/uncheck.png");
		}
		else
		{
			$("#RememberPass").prop("checked", "checked");
			//$(".checkbox-icon").addClass("checked");
			$(".checkbox-icon").attr("src","images/check.png");
		}
	});
	
	if(getCookie("isRememberPass") == "true")
	{
		if(!$("#RememberPass").is(":checked"))
		{
			$("#RememberPass").prop("checked", "checked");
			//$(".checkbox-icon").addClass("checked");
			$(".checkbox-icon").attr("src","images/check.png");
		}
		else
		{
			$(".checkbox-icon").attr("src","images/check.png");
		}
	}

	$("#login").click(function(e) {
		e.preventDefault();
		if($("#login").attr("disabled")) {
			return;
		}
		if($("#loginForm").form("validate")){
			$("#login").attr("disabled", "disabled");
		
			$("#login").val("登录中...");
			//$("#loginForm").submit();
			//if(key)
			//{
				startLogin();
			//}
			
		}
	});
	$("#username").focus();
});

function startLogin() {
	var loginInfoJson = {};
	loginInfoJson["username"] = $("#username").val();
	//loginInfoJson["password"] = window.btoa($("#password").val());
	if(g_isNeedEncrypt)
	{
		loginInfoJson["password"] = RSAUtils.encryptedString(key,$("#password").val());
	}
	else
	{
		//loginInfoJson["password"] = $("#password").val();
		loginInfoJson["password"] = getCookie("encrypt_p");
	}
	
	$.ajax({
		type: "post",
		url: "/api/login",
		data: JSON.stringify(loginInfoJson),
		contentType: "application/json",
		success: function(result) {
			if(result.resultCode != "SUCCESS")
			{
				$("#login").removeAttr("disabled");
				$("#login").val("登录");
				if(result.resultCode == "USER_NAME_OR_PWD_INVAILD")
				{
					$.messager.alert("错误", "<p>登录失败，用户名或密码错误！</p>", "error");
				}
				else if(result.resultCode == "USER_DISALLOW")
				{
					$.messager.alert("错误", "<p>登录失败，该账号未启用！</p>", "error");
				}
				else if(result.resultCode == "USER_UNAUTHORIZED")
				{
					$.messager.alert("错误", "<p>登录失败，该账号未授权！请等待管理员授权</p>", "error");
				}
				else
				{
					$.messager.alert("错误", "<p>登录失败！</p>", "error");
				}
			}
			else
			{
				//alert("=======");
				setCookie("isRememberPass", $("#RememberPass").is(":checked"), 30);
				if($("#RememberPass").is(":checked"))
				{
					setCookie("username", $("#username").val(), 30);
					setCookie("encrypt_p", loginInfoJson["password"], 30);
					setCookie("p_len", $("#password").val().length, 30);
				}
				else
				{
					delCookie("username");
					delCookie("encrypt_p");
				}
				if(window.parent.tabs)
				{
					var selTabTitle = $(".tabs-header .tabs .tabs-selected .tabs-title", $(window.parent.tabs)).text();
					
					$(".st_tree a", $(window.parent.document.body)).each(function(){
						if($(this).text() == selTabTitle)
						{
							window.location.href = $(this).attr("href");
						}
					});
				}
				else
				{
					window.location.href = "index.html";
				}
			}
			
		},
		error: function() {
			$("#login").removeAttr("disabled");
			$("#login").val("登录");
			$.messager.alert('错误','<p>服务异常，登录失败，请稍后重试!</p>', 'error');
		}
	});
}

function clearAutofillStyle() {
	if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
		setTimeout(function(){
			//删除旧版本的cookie
			if(getCookie("password")) {
				$("#password").val(getCookie("password"));
				g_isNeedEncrypt = true;
				delCookie("password");
			}
			if(getCookie("encrypt_p")) {
				//$("#password").val(getCookie("encrypt_p"));
				$("#password").val(getRandomFakePsw());
				g_isNeedEncrypt = false;
			}
			if($('input:-webkit-autofill').length)
			{
				$('input:-webkit-autofill').each(function(){
					var text = $(this).val();
					var name = $(this).attr('name');
					$(this).after(this.outerHTML);//.remove(); 
					$(this).val("");
					$(this).attr("id", "");
					$(this).attr("name", "");
					$(this).hide();
					//$('input[name=' + name + ']').val(text);
					$('#username, #password').bind('input propertychange', function() {
					    if($(this).val() != "")
					    {
					    	$(".placeholder", $(this).parent()).hide();
					    }
					    else
					    {
					    	$(".placeholder", $(this).parent()).show();
					    }
					    //clearAutofillStyle();
					});
					$('input[name=' + name + ']').val(text);
					setTimeout(function(){
						$('#username, #password').validatebox({
							required: true
						});
						$('#password').change(function(){
							g_isNeedEncrypt = true;
						});
						$("#loginForm").form("validate");
						$('#username').focus();
					},10);
					
				});
			}
			else
			{
				$('#username, #password').validatebox({
					required: true
				});
				$('#password').change(function(){
					g_isNeedEncrypt = true;
				});
				$("#loginForm").form("validate");
				$('#username').focus();
			}
		},150);
	}
	else
	{
		$('#username, #password').validatebox({
			required: true
		});
		$('#password').change(function(){
			g_isNeedEncrypt = true;
		});
		$("#loginForm").form("validate");
	}
}

function getCookie(c_name)
{
	var cookieStr = document.cookie.replace(/\s/g, '');
	if (cookieStr.length>0)
	{ 
		var cookieStrArr = cookieStr.split(";");
		var cookieObj = {};
		for(var i = 0; i < cookieStrArr.length; ++i)
		{
			var keyValuePair = cookieStrArr[i].split("=");
			if(keyValuePair[0])
			{
				cookieObj[keyValuePair[0]] = keyValuePair[1];
			}
		}
		var cookieValue = cookieObj[c_name];
		if(cookieValue)
		{
			return unescape(cookieValue);
		}
		/*c_start=cookieStr.indexOf(c_name + "=");
		if (c_start!=-1 && cookieStr.charAt(c_start - 1) == '' ||)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=cookieStr.indexOf(";",c_start);
			if (c_end==-1) 
				c_end=document.cookie.length;
			
			return unescape(document.cookie.substring(c_start,c_end));
		}*/ 
	}
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

function delCookie(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval=getCookie(name);
	if(cval!=null)
		document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

function getRandomFakePsw() {
	var psw = getCookie("encrypt_p");
	var originPswLen = parseInt(getCookie("p_len"), 10);
	if(originPswLen)
	{
		psw = "";
		var randomCharArr = ["!","@","#","$","%","^","&","*","(",")"];
		for(var i = 0; i < originPswLen; ++i)
		{
			var randomNum = parseInt(Math.random() * 10, 10);
			//psw += randomNum;
			psw += randomCharArr[randomNum];
		}
	}
	return psw;
}
</script>
</body>
</html>
